import { i18n } from "@@/language";
import { copyText } from "@@/utils/tools/project";
import { showDialog } from "vant";
import { defaHttp, Api } from "../index";

//@ts-ignore
const { t } = i18n.global;
/**swap(兑换)提交 */
export async function apiSwapTxid(data: ApiSwapTxid.Req): Promise<boolean> {
  const res = await defaHttp.post<Api.Res<ApiSwapTxid.Data>>("/swap/insert", data, {
    mode: {
      successMessage: true,
      failMessage: true,
      loadingMessage: t("tong-bu-jiao-yi-zhong-qing-bu-yao-tui-chu"),
    },
  });
  if (res.code !== 200) return false;
  return true;
}
/**swap(兑换)提交 */
export namespace ApiSwapTxid {
  export interface Req {
    txid: string;
    /**数量 */
    number: number;
    from_id: number;
    to_id: number;
  }
  export type Data = void;
}

function txidFail<T>(
  api: Function,
  params: AnyObject[],
  txid: string
): Promise<boolean | T> {
  return new Promise<boolean | T>((resolve) => {
    showDialog({
      title: t("txid-fail-title"),
      message: `${t("txid-fail-text")}${txid}`,
      confirmButtonText: t("chong-shi"),
      cancelButtonText: t("fu-zhi-ha-xi"),
    })
      .then(() => {
        resolve(api(...params));
      })
      .catch(() => {
        copyText(txid);
        resolve(false);
      });
  });
}
